"""
主题: 创建新的类或实例属性
问题: 你想创建一个新的拥有一些额外功能的实例属性类型，比如类型检查。
提示 : 
"""

class Integer:
    '''描述器类'''    
    def __init__(self, name):
        self.name = name

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if not isinstance(value, int):
            raise TypeError('Property "{}" expected an int'.format(self.name))
        instance.__dict__[self.name] = value

    def __delete__(self, instance):
        del instance.__dict__[self.name]


class Point:
    x = Integer('x')
    y = Integer('y')

    def __init__(self, x, y):
        self.x = x
        self.y = y


def recipe1():
    print(f"{Point.x = }")

    p = Point(2, 3)
    print(f"{p.x = }")
    p.y = 5
    print(f"{p.y = }")

    try:
        p.x = 'a'
    except TypeError as e:
        print(e)      

def main():
    print('recipe1'.center(20, '*'))
    recipe1()

if __name__ == '__main__':
    main()                     